<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\components\Loyalty;
use app\components\Log;

class WechatPayController extends Controller{
    
    public function actionIndex()
    {
        $postStr = $GLOBALS['HTTP_RAW_POST_DATA'];
//         libxml_disable_entity_loader(true);
//         $re = json_decode(json_encode(simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA)), true);Log::debug('PAY DATA', $resStr);
//         $xmlData = file_get_contents('php://input');
//         file_put_contents('test.text',$xmlData);
        Log::debug('WX PAY',$postStr);
//         $res = Loyalty::transaction_redeem(['code' => '18227693267', 'campaign_id' => 3, 'points_to_redeem' => 10]);
        Log::debug('WX TEST',123);
//         echo json_encode($res);
//         $xmlData = file_get_contents('php://input');
//         libxml_disable_entity_loader(true);
//         $data = json_decode(json_encode(simplexml_load_string($xmlData, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
//         Log::debug('PAY DATA', $data);
//         ksort($data);
//         $buff = '';
//         foreach ($data as $k => $v){
//             if($k != 'sign'){
//                 $buff .= $k . '=' . $v . '&';
//             }
//         }
//         $stringSignTemp = $buff . 'key=54e7209f2cd5408d679e546a99e95c51';//key为证书密钥
//         $sign = strtoupper(md5($stringSignTemp));
//         //判断算出的签名和通知信息的签名是否一致
//         if($sign == $data['sign']){
//             //处理完成之后，告诉微信成功结果
//             $re = Loyalty::transaction_redeem(['code' => '18227693267', 'campaign_id' => 3, 'points_to_redeem' => 10]);
//             return sprintf("<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>");     
//         }
    }
}